var app = new Vue({
    el: "#app",
    data: {
        form: {
            applyId: null,
            realName: null,
            idType: "1",
            idNumber: null,
            phoneNumber: null,
            serviceType: null,
            state: "1",
            hallId: null,
            dayId: null,
            timePeriod:null
        },
        timeList:[0,0,0,0],
        monthList: [],
        monthId: "",
        step: 1,
        dayName: ""
    }, methods: {
        chooseServiceType: function (serivceType) {
            this.form.serviceType= serivceType;
            this.step = 2;
            this.form.hallId = null;
        },
        chooseHall: function (hallId) {

            var that = this;
            this.monthList = [];
            $.post("/appointment/monthList", {hallId:hallId,serviceType:this.form.serviceType}, function (result) {
                that.monthList = result;
                that.monthId = result[0].monthId;
            });

            this.form.hallId =hallId;
            this.step = 3;
            this.form.dayId = null;
        },
        chooseDay: function (dayId,dayName,dayObject) {
            if(dayObject.appFlag)
            {
                this.form.dayId = dayId;
                this.dayName = dayName;
                this.step = 4;
                this.form.timePeriod = null;

                for(var m in this.monthList)
                {
                    var dayList = this.monthList[m].dayList;
                    for(var d in dayList)
                    {
                        if(dayList[d].dayId==this.form.dayId)
                        {
                            this.timeList = dayList[d].timeList;
                        }
                    }
                }
            }
        },
        chooseTime: function (timePeriod) {
            if(this.timeList[timePeriod]>0)
            {

                this.form.timePeriod = timePeriod;
                this.step = 5;
            }
        },
        modifyItem: function (step) {
            this.step = step;
        },
        showPrevMonth:function () {
            for(var i = 0;i<this.monthList.length;i++)
            {
                if(this.monthId == this.monthList[i].monthId && this.monthList[i].hasPrev)
                {
                    this.monthId = this.monthList[i-1].monthId;
                    break;
                }
            }
        },
        showNextMonth:function () {
            for(var i = 0;i<this.monthList.length;i++)
            {
                if(this.monthId == this.monthList[i].monthId && this.monthList[i].hasNext )
                {
                    this.monthId = this.monthList[i+1].monthId;
                    break;
                }
            }
        },
        submit:function () {
            if (this.validateInput()){
                $.post("/appointment/add",this.form,function (res) {
                    if(res.result=="ok")
                    {
                        showDialog("预约成功!在预约时间段内凭预约身份证件到指定接待大厅可以优先办理!");
                    }
                });
            }
        },
        validateInput:function () {
            if(!this.form.applyId)
            {
                if(!checkNull(this.form.realName))
                {
                    showDialog("请填写正确的预约人姓名");
                    return false;
                }


                if(!checkNull(this.form.idNumber))
                {
                    showDialog("请填写正确的证件号码");
                    return false;
                }

                if(!checkPhone(this.form.phoneNumber))
                {
                    showDialog("请填写正确的手机号码");
                    return false;
                }
            }
            return true;
        }
    },
    created: function () {
        var that = this;
        if ($("#applyId").val()) {
            that.form.applyId = $("#applyId").val();
            that.form.realName = $("#realName").val();
            that.form.idNumber = $("#idNumber").val();
            that.form.phoneNumber = $("#phoneNumber").val();
            that.form.serviceType = $("#serviceType").val();
            that.step=2;
        }


    },
    computed: {
        hallList:function(){
            if (this.form.serviceType==null)
            {
                return [];
            }
            var result = [];

            for (h in allHallList)
            {
                if(this.form.serviceType == 'A' && allHallList[h].serviceTypeA1=='1')
                {
                    result.push(allHallList[h]);
                }

                if(this.form.serviceType == 'B' && allHallList[h].serviceTypeB1=='1')
                {
                    result.push(allHallList[h]);
                }

                if(this.form.serviceType == 'C' && allHallList[h].serviceTypeC1=='1')
                {
                    result.push(allHallList[h]);
                }
            }
            return result;
        },
        beforeStep1: function () {
            return this.step < 1;
        },
        beforeStep2: function () {
            return this.step < 2;
        },

        beforeStep3: function () {
            return this.step < 3;
        },

        beforeStep4: function () {
            return this.step < 4;
        },

        beforeStep5: function () {
            return this.step < 5;
        },
        onStep1: function () {
            return this.step == 1;
        },
        onStep2: function () {
            return this.step == 2;
        },

        onStep3: function () {
            return this.step == 3;
        },

        onStep4: function () {
            return this.step == 4;
        },

        onStep5: function () {
            return this.step == 5;
        },
        afterStep1: function () {
            return this.step > 1;
        },
        afterStep2: function () {
            return this.step > 2;
        },
        afterStep3: function () {
            return this.step > 3;
        },
        afterStep4: function () {
            return this.step > 4;
        },
        afterStep5: function () {
            return this.step > 5;
        },
        serviceName: function () {
            var result =  '';
            switch (this.form.serviceType) {
                case 'A':
                    result = '公民业务';
                    break;
                case 'B':
                    result = '境外业务';
                    break;
                case 'C':
                    result = '综合业务';
                    break;
            }
            return result;
        },
        hallName:function () {
            for (h in allHallList)
            {
                if(allHallList[h].id==this.form.hallId)
                {
                    return allHallList[h].hallName;
                }
            }
        },
        timeName:function () {
            if(this.form.timePeriod==0)
            {
                return "09:00 - 10:00";
            }
            if(this.form.timePeriod==1)
            {
                return "11:00 - 11:30";
            }
            if(this.form.timePeriod==2)
            {
                return "14:00 - 15:00";
            }
            if(this.form.timePeriod==3)
            {
                return "15:30 - 16:30";
            }
            return "";
        }
        // ,
        // checkTime9:function () {
        //     return this.checkTime(9)
        // },
        // checkTime10:function () {
        //     return this.checkTime(10)
        // },
        // checkTime11:function () {
        //     return this.checkTime(11)
        // },
        // checkTime14:function () {
        //     return this.checkTime(14)
        // },
        // checkTime15:function () {
        //     return this.checkTime(15)
        // },
        // checkTime16:function () {
        //     return this.checkTime(16)
        // }
        

    }
});